import {Classify} from "../../models/classify";
import {Information} from "../../models/information";
import {city, dateToTimestamp, promisic} from "../../util/util";
import {config} from "../../config/config";
import {showToast} from "../../util/ui";

Page({
  data: {
    swiperArray: [{
      url: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.pinda.net.cn%2Fpic%2Fallimg%2Fcopyright%2Fimg%2Fb7a8c2c91_2.jpg&refer=http%3A%2F%2Fwww.pinda.net.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648281343&t=26bc604782069ab37e39b20020e5f214',
      id: '001'
    }],
    gird: [],
    gridIndex: 0,
    gridItemText: '全部',
    columns: city,
    currentCity: '全国',
    isShowPicker: false,
    list: [],
    classifyQuery: {
      pageNum: 1,
      pageSize: 30
    },
    informationQuery: {
      pageNum: 1,
      pageSize: 5,
      region: '',
      classifyName: '',
    },
    listTotal: 0, // 列表总数
    showLoading: true,
    lock: false
  },
  async onLoad(options) {
    await this.getTypes()
    await this.getInformationList(true)
  },
  async getTypes() {
    const res = await Classify.doApiGetClassifyList(this.data.classifyQuery)
    const list = res.rows.map(i => {
      return i.name
    })
    list.unshift('全部')
    this.setData({
      gird: list
    })
  },
  async getInformationList(isEmpty) {
    this.setData({
      showLoading: true,
      lock: true,
      list: isEmpty ? [] : this.data.list
    })
    const res = await Information.doApiGetInformationList(this.data.informationQuery)
    res.rows.forEach(item => {
      item.tagTime = dateToTimestamp(item.createTime)
    })
    this.setData({
      list: this.data.list.concat(res.rows),
      listTotal: res.total,
      showLoading: false,
      lock: false
    })
  },
  //点击宫格item
  async onGridItem(event) {
    //点击item 下标索引
    const index = event.currentTarget.dataset.index
    // 点击项
    const item = event.currentTarget.dataset.item
    this.setData({
      gridIndex: index,
      gridItemText: item,
      "informationQuery.classifyName": item === '全部' ? '' : item
    })
    await this.getInformationList(true)
  },
  //点击picker确认
  async onConfirm(event) {
    const {
      value,
      index
    } = event.detail;
    this.setData({
      currentCity: value,
      isShowPicker: false,
      "informationQuery.region": value === '全国' ? '' : value
    })
    await this.getInformationList(true)
  },
  //开启picker
  showPicker() {
    this.setData({
      isShowPicker: true
    })
  },
  // 点击取消picker
  onCancel() {
    this.setData({
      isShowPicker: false
    })
  },
  // 点击卡片详情
  async onCardDetail(event) {
    this.setData({
      showLoading: true
    })
    const id = event.currentTarget.dataset.id
    const res = await Information.doApiGetInForListById(id)
    await this.openFile(res.data.enclosure)
    this.setData({
      showLoading: false
    })
    // wx.navigateTo({
    //   url: `../successful-detail/successful-detail?id=${id}`,
    // })
  },
  async openFile(fileUrl) {
    const completeFileUrl = config.apiBaseUrl + fileUrl
    try {
      const fileTempRes = await promisic(wx.downloadFile)({
        url: completeFileUrl
      })
      const fileTempPath = fileTempRes.tempFilePath
      if (!fileTempPath) return showToast('解析文件出现错误')
      await wx.openDocument({
        filePath: fileTempPath
      })
    } catch (e) {
      showToast('解析文件出现错误')
    }
  },
  async onReachBottom() {
    const listLength = this.data.list.length
    if (this.data.listTotal <= listLength) {
      showToast('没有更多数据了')
      return
    }
    this.setData({
      "informationQuery.pageNum": this.data.informationQuery.pageNum++
    })
    if (this.data.lock) return
    await this.getInformationList(false)
  }
})
